//= require <framework/w3c/components/AbstractView>
//= require <framework/libs/templating/EcmaScriptTemplates>
//= require <framework/models/ComponentModel>
//= require <framework/w3c/traits/CSSStyleUtilities>
//= require <framework/w3c/traits/Draggable>

namespace("w3c.HtmlView", {
    '@inherits': w3c.AbstractView,
	'@traits'  : [w3c.CSSStyleUtilities, Draggable],
	
	
	preInitialize : function(model, element) {
		var args = arguments;
		try {
			this.setModel(model);
        	this.setElement(element);
			this.initialize(args); 
		} 
		catch(e){
			alert(this.namespace + "#initialize() cause an error:\n" + e.message);
		}
		return this;
    },
	
	innerHTML : "<div></div>",
	
	setModel : function(jsonobj) {
		this.model = (jsonobj && jsonobj instanceof jswing.models.ComponentModel) ?
			jsonobj : this.defineModel(jsonobj||{});
	},
	
	defineModel : function(json){
		return new jswing.models.ComponentModel(json||{});
	},
	
	setElement : function(element){
		this.element = (element || this._internal_parseElement());
		this.setElementPrototype();
		this.loadcss();
	},
	
	setElementPrototype : function(){
		this.element.prototype = this;
	},
	
	get : function(key){
		return this.model.get(key)
	},
	
	set : function(key,val) {
		this.model.set(key,val);
	},
	
	parseTemplate : function (template, json){
		if(typeof json !== "object") {
		    throw new Error("w3c.HtmlView#parseTemplate(template, json) expects a\
			valid json object-literal for 2nd argument.");
		};
		return (TemplateEngine.parse(template,null,{$:json, self:this}) || "");
	},

	parseElement : function (template, json){
	     var html = this.parseTemplate(template, json);
		 if (html && html.length > 0) {return html.toHtmlElement()}
		 else {
		 	throw new Error("w3c.HtmlView#parseElement(template, json). Invalid xhtml\
			generated from 'template' string. Value of 'html' is: "+ html);
		 }
	},
	
    resourcepath : function(filepath){
		var nspath = this.namespace.replace(/\./g,"/") + "/";
		if (filepath.indexOf("/") === 0 || filepath.indexOf("http://") === 0) {
			return filepath;
		}
		else {
			if (window.config.loadpaths.CDN && !(filepath.indexOf("http://") === 0)) {
				return window.config.loadpaths.CDN + nspath + filepath;
			}
			else {
				return config.loadpaths.LIBROOT + nspath + filepath;
			}
		}
    },
	
	_internal_parseElement : function(){
		var templateString = (typeof this.innerHTML === "function") ?
			this.innerHTML() : this.innerHTML;
		return this.parseElement(templateString, {});
	},
	
	confirm : function(msg, callback, options){
		var node=null;
		if(Object.isElement(msg)) {
			node=msg;
		}
		else if(typeof msg === "string") {
			var node = document.createElement("div");
	        node.innerHTML = 
					"<div class='msg'>" + msg + "</div>\
	                 <span class='dismissal-controls'>\
					 	<span class='button button-ok flat-rounded-button'>OK</span>\
						<span class='button button-cancel flat-rounded-button'>CANCEL</span>\
					 </span>";
		};
		if(!node) {throw new Error(this.namespace + "#confirm(obj, callback, options) expected either a String or Node reference as the 1st param")};
		if(!this.hasClass("ConfirmDialog", node)) {
			this.addClass("ConfirmDialog",node);
		};
		
        var m = new controls.ModalDialog(node, callback, options||{});
		m.show();
	}
});